package com.qsw.paper.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsw.paper.pojo.dto.ClassesDto;
import com.qsw.paper.pojo.dto.ClassesQueryDto;
import com.qsw.paper.pojo.entity.Classes;
import com.qsw.paper.mapper.ClassesMapper;
import com.qsw.paper.result.PageResult;
import com.qsw.paper.service.IClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qsw
 * @since 2024-11-15
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements IClassesService {

    /**
     * 分页查询班级
     *
     * @param queryDto dto
     * @return 分页列表
     */
    @Override
    public PageResult<?> pageList(@NotNull ClassesQueryDto queryDto) {
        Integer advisorId = queryDto.getAdvisorId();
        Integer collegeId = queryDto.getCollegeId();
        String className = queryDto.getClassName();
        //开启分页
        Page<Classes> classesPage = page(new Page<>(queryDto.getPageNum(), queryDto.getPageSize()), new LambdaQueryWrapper<Classes>()
                .eq(advisorId != null, Classes::getAdvisorId, advisorId)
                .like(StringUtils.isNotBlank(className), Classes::getClassName, className)
                .eq(collegeId != null, Classes::getCollegeId, collegeId)
                .orderByDesc(Classes::getCreatedAt));
        return new PageResult<>(classesPage.getTotal(), classesPage.getRecords());
    }

    /**
     * 添加班级
     *
     * @param classesDto dto
     */
    @Override
    public void save(ClassesDto classesDto) {
        //添加班级信息
        save(BeanUtil.copyProperties(classesDto, Classes.class).setCreatedAt(LocalDateTime.now()));
    }

    /**
     * 修改班级信息
     *
     * @param classesDto dto
     * @param id         班级id
     */
    @Override
    public void update(ClassesDto classesDto, Integer id) {
        //修改班级信息
        baseMapper.updateById(BeanUtil.copyProperties(classesDto, Classes.class).setClassId(id));
    }
}
